//
//  QCHomeViewModel.swift
//  SwiftDemoWithSina
//
//  Created by qu on 2017/2/4.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit
import SDWebImage

class QCHomeViewModel: NSObject {

    //声明一个全局 的数组 保存数据
    lazy var dataArray:[QCStatusViewModel] = [QCStatusViewModel]()
    
}

extension QCHomeViewModel{
    
    func getHomeData(isPullUP:Bool,isFinish:@escaping (Bool,Int)->()){
        
        //定义sinceID  和 maxID 默认为0
        var sinceId: Int64 = 0
        var maxId: Int64 = 0
        
        //判断 是上拉 刷新 还是下拉加载
        if isPullUP {
            maxId = dataArray.last?.homeModel?.id ?? 0
            
            if maxId > 0 {
                maxId = maxId - 1
            }
            
        }else {
            sinceId = dataArray.first?.homeModel?.id ?? 0
        }
        
    QCNetworkTools.shared.loadHomeData(since_id:sinceId, max_id:maxId,success: { (response) in
       
         //判断 response 是否为nil 而且是否可以转成字典
            guard let resDict = response as? [String:Any] else {
           //执行闭包并返回
                isFinish(false,0)
                return
            }
            //是否为空数组
            guard let res = resDict["statuses"] else {
                isFinish(false,0)
                return
            }
            //得到一个模型数组
          let statusArray = NSArray.yy_modelArray(with: QCHomeModel.self, json: res) as![QCHomeModel]
            //创建一个临时可变数组
            var tempArray:[QCStatusViewModel] = [QCStatusViewModel]()
            
            let group = DispatchGroup()
            //遍历statusArray 内的模型数据 创建statusViewModel 给其对应的homeModel 赋值  最后保存到临时数组中
            for homeModel in statusArray {
                
                if homeModel.pic_urls?.count == 1 {
                    
                    group.enter()
                    SDWebImageManager.shared().downloadImage(with: URL(string: homeModel.pic_urls?.first?.thumbnail_pic ?? ""), options: [], progress: nil, completed: { (image, error, _, _, _) in
                        print("原创微博单张图片下载完成")

                        group.leave()
                    })
                    
                }
                
                if homeModel.retweeted_status?.pic_urls?.count == 1 {
                    
                    group.enter()
                    SDWebImageManager.shared().downloadImage(with: URL(string: homeModel.retweeted_status?.pic_urls?.first?.thumbnail_pic ?? ""), options: [], progress: nil, completed: { (image, error, _, _, _) in
                        print("转发微博单张图片下载完成")
                        
                        group.leave()
                    })
                    
                }
                
                let statusViewModel = QCStatusViewModel()
                //对属性赋值
                statusViewModel.homeModel = homeModel
                //添加到临时数组
                tempArray.append(statusViewModel)
            }
//          self.dataArray = tempArray
        
        if isPullUP {
            self.dataArray = self.dataArray + tempArray
        }else {
            self.dataArray = tempArray + self.dataArray
        }
        
          group.notify(queue: DispatchQueue.main, execute: { 
            print("所有微博单张图片下载完成")
            //执行闭包
            isFinish(true,tempArray.count)
            
          })
            
        },failure:{ (error) in
            print("失败",error)
            isFinish(false,0)
        })
    }
}
